package com.zhiche.lisa.bms.controller.utils;

import com.zhiche.lisa.bms.enums.BaseParamKeyEnum;
import com.zhiche.lisa.bms.service.base.BaseCommonParameService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.core.utils.Account.AccountUtil;
import com.zhiche.lisa.core.utils.Account.JwtAccountVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: linbao
 * @date: 2019-06-04
 */
@RestController
@RequestMapping("/userBaseCommon")
@Slf4j
public class UserBaseCommon {

    @Autowired
    private BaseCommonParameService baseCommonParameService;

    /**
     * 考核和奖励单审核人权限名单
     *
     * @return
     */
    @GetMapping("/verifyUserPermission")
    public RestfulResponse<Object> verifyUserPermission() {
        log.info("UserBaseCommon.verifyUserPermission ");
        RestfulResponse<Object> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            JwtAccountVO account = AccountUtil.getAccountInfoFromSecurityContext();
            if (account == null) {
                throw new BaseException("无登录信息");
            }
            List<String> valueList = baseCommonParameService.getBaseCommonValueByKey(BaseParamKeyEnum.ASSESS_REWARD_KEY.getKey());
            if (CollectionUtils.isEmpty(valueList) || !valueList.contains(account.getUsername())) {
                restful.setCode(2);
            }

            // if (!UserCommon.userList.contains(account.getUsername())) {
            //     restful.setCode(2);
            // }
        } catch (BaseException e) {
            logAndResultMsg(restful, e.getMessage(), "UserBaseCommon.verifyUserPermission error: {}", e.getMessage());
        } catch (Exception e) {
            logAndResultMsg(restful, CommonEnum.ERROR.getText(), "UserBaseCommon.verifyUserPermission error: {}", e.getMessage());
        }
        return restful;
    }


    private void logAndResultMsg(RestfulResponse restfulResult, String errorMsg, String logParam, String logErrorMsg) {
        log.error(logParam, logErrorMsg);
        restfulResult.setCode(CommonEnum.ERROR.getCode());
        restfulResult.setMessage(errorMsg);
    }
}
